<?php
			ob_start();			
?>
<?php
		
		if (isset($_COOKIE["user"])) {
			// if cookie <> ""
		  }
		  else
		  {
			  header("location:login.php");
			  
		  }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="css/form_css.css" rel="stylesheet" type="text/css"  />
</head>
<?php
function updatePublish($published)
{	
	$published = $published == 1 ? 0 : 1;
	$pro_id_pub = $_REQUEST["pro_id_pub"];
	$sql = "UPDATE tbl_product SET published={$published} WHERE pro_id=$pro_id_pub";
	query($sql);	
}
?>
<?php
if(isset($_REQUEST["published"]))
{
	updatePublish($_REQUEST["published"]);
	
}
?>

<?php
function updateShowFront($showFront)
{	
	$showFront = $showFront == 1 ? 0 : 1;
	$pro_id_showFront = $_REQUEST["pro_id_showFront"];
	$sql = "UPDATE tbl_product SET show_front={$showFront} WHERE pro_id=$pro_id_showFront";
	query($sql);	
}
?>
<?php
if(isset($_REQUEST["showFront"]))
{
	updateShowFront($_REQUEST["showFront"]);
	
}
?>
<?php
function deleteRecord($id_imagename)
{
	//get id from array
	$pro_id = key($id_imagename);
	//get cat_image from array
	$imagename = current($id_imagename);
	$sql = "DELETE FROM tbl_product WHERE pro_id={$pro_id}";
	$result = query($sql);	
	if($result)
	{
		//delete image front page
		deleteFile("product/{$imagename}");
		//delete image detail page
		$imagename_d=$imagename."-d.jpg";
		deleteFile("product/{$imagename_d}");
		
		//delete image gallery
		$imagename_g=$imagename."-g.jpg";
		deleteFile("product/{$imagename_g}");

		
		//*** Loop Delete gallery ***//
		$strSQL2 = "SELECT * FROM tbl_gallery WHERE pro_id = '".$pro_id."' ORDER BY gal_id DESC ";
		$objQuery2 = mysql_query($strSQL2) or die ("Error Query [".$strSQL2."]");
		while($objResult2 = mysql_fetch_array($objQuery2))
		{
			if($objQuery2)
			//*** Delete image gallery (gallery) ***//			
			deleteFile("gallery/".$objResult2["photo"]);
			deleteFile("gallery/".$objResult2["photo"]."-s.jpg");
		}
		
		//*** Delete All Rows  (gallery)***//
		$strSQL = " DELETE FROM tblgallery ";
		$strSQL .=" WHERE pro_id = '".$pro_id."' ";
		$objQuery = mysql_query($strSQL);		

		
		//end delete all gallery
		errorMessage("Supprimer réussies!");
	}	
	else
	{
		errorMessage("Supprimer échec ..!");
	}	
}
?>
<?php
	if(@is_array($_REQUEST["pro_id"]))
{
	$id_imagename = $_REQUEST["pro_id"];
	deleteRecord($id_imagename);
}
?>
<body>
                        <div id="border_cont_title">
                        	<div id="content_title_left">Gestion des produits</div>
                            <div id="content_title_right"><a href="index.php?page=productNew">nouveau produit</a></div>
                        </div>
                            <!--start table -->
                            <table width="100%">
                                <tr>
                                	<td colspan="7">
                        <form  method="get" id="frmsearch">
                    	 produit:
                   	      <input type="text" name="txtproduct" size="15" value="<?php echo @$search_text=$_GET["txtproduct"];?>" onchange="frmSubmit('frmsearch','index.php?page=product');"  />
                        Catégorie:
                        <select name="cbocat" onchange="frmSubmit('frmsearch','index.php?page=product');">
               	    <option value=""> -- Toutes les caté -- </option>
								<?php
                                    $sql = "SELECT * FROM tbl_category WHERE public=1";
                                    $result = query($sql);
                                    while($row = fetch_assoc($result))
                                    {
                                        
                                        if(@$search_cat=$_REQUEST["cbocat"] == $row['cat_id'])
                                            $selected = " selected='selected'";
                                        else	
                                            $selected = "";
                                ?>
                                        <option value="<?php echo $row['cat_id'];?>"<?php echo $selected;?>>
                                        <?php echo $row['cat_name'];?></option>
                                <?php
                                    }
                                    
                                ?>
                        </select>
                        Statut:
                        <select name="cbostatus" onchange="frmSubmit('frmsearch','index.php?page=product');">
               	    <option value=""> -- Tout d'état -- </option>
								<?php
                                    $sql = "SELECT * FROM tbl_status";
                                    $result = query($sql);
                                    while($row = fetch_assoc($result))
                                    {
                                        
                                        if(@$status=$_REQUEST["cbostatus"] == $row['sta_id'])
                                            $selected = " selected='selected'";
                                        else	
                                            $selected = "";
                                ?>
                                        <option value="<?php echo $row['sta_id'];?>"<?php echo $selected;?>>
                                        <?php echo $row['sta_name'];?></option>
                                <?php
                                    }
                                    
                                ?>
                        </select>
                        <input  class="submit" type="submit" name="btnsubmit" value="Rechercher"  />
                        <a href="index.php?page=product" style="color:#F00;">Effacer</a>
                      </form>
                                    </td>
                                </tr>
                            	<tr>
                                	<th>Produit</th>
                                    <th>Catégorie</th>
                                    <th>​​Prix</th>
                                    <th>Statut</th>
                                    <th>Voir avant</th>
                                    <th>Publier</th>
                                    <th>Action</th>
                                </tr>
                              <?php 
									//include function from cls_pag
								include('../includes/cls_pagination.php');
								
								
								@$search_text=$_REQUEST["txtproduct"];
								@$search_text=$_REQUEST["txtproduct"];
								
								@$search_cat=$_REQUEST["cbocat"];
								@$search_cat=$_REQUEST["cbocat"];
								
								@$status=$_REQUEST["cbostatus"];
								@$status=$_REQUEST["cbostatus"];
								
								if($search_text=="" && $search_cat=="" && $status=="")
								{
									include('search_product/default.php');
									
								}//end search text ==""
								elseif($search_text!="")
							    {
									include('search_product/search_pro.php');
								}//end else if $search_text <>""
								elseif($search_cat !="")
							    { 
									include('search_product/search_cat.php');
									
								}//end else if $search_cat <>""
							
								elseif($status !="")
								{ 
									include('search_product/search_status.php');
								}//end else if $status <>""
								
							?>                              
							
</body>
</html>
